// pages/insomniaTest/answer/answer.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 1：A, 2:B,3:C
        selectIndex: 2,
        // 进度
        percentage: 2,
        // 题库数据
        tikuList: [],
        // 当前作答的题目item
        itemtikuList: {},
    },

    // 完成
    gotoNext() {
        wx.navigateTo({
            url: '../testsComplete/testsComplete',
        })
    },

    /**
     * 上一题
     * @param {*} e 
     */
    sahngyiti() {
        const itemtikuListId = this.data.itemtikuList.index
        if (itemtikuListId == 1) {
            // 已到第一题
            wx.showToast({
                title: '已经是第一题！',
                icon: 'none'
            })
            return;
        }
        let itemtikuListNext = {};
        this.data.tikuList.forEach((o) => {
            if (o.index === itemtikuListId - 1) {
                itemtikuListNext = o;
                return;
            }
        })
        this.setData({
            itemtikuList: itemtikuListNext,
            selectIndex: itemtikuListNext.selectIndex,
            percentage: (itemtikuListNext.id * 2.7).toFixed(0),
        })
    },

    /**
     * 点击答案项(下一题)
     */
    onClickItem(e) {
        const selectIndexNow = e.target.dataset.num;
        this.setData({
            selectIndex: selectIndexNow,
        })
        const itemtikuListNow = this.data.itemtikuList
        itemtikuListNow.selectIndex = selectIndexNow;
        const itemtikuListId = this.data.itemtikuList.index
        let itemtikuListNext = {};
        this.data.tikuList.forEach((o, index) => {
            if (o.index == itemtikuListId) {
                o.selectIndex = selectIndexNow;
                // 记录当前答题到哪里
                wx.setStorageSync('nowmydatiitemindex', index)
            }
            if (o.index === itemtikuListId + 1) {
                itemtikuListNext = o;
            }
        })
        // 缓存到本地
        wx.setStorageSync('mytikuData', this.data.tikuList)
        const myset = setTimeout(() => {
            if (itemtikuListId == 38) {
                // 最后一题-完成
                this.gotoNext();
            } else {
                // 跳到下一项
                console.log('跳到下一项');
                this.setData({
                    itemtikuList: itemtikuListNext,
                    selectIndex: itemtikuListNext.selectIndex,
                    percentage: (itemtikuListNext.index * 2.63).toFixed(0),
                })
            }
            clearTimeout(myset)
        }, 300)
    },

    // 获取题库数据
    async getdaanList() {
        let locaTikuData = await wx.getStorageSync('mytikuData')
        const mytikuData_copy = wx.getStorageSync('mytikuData_copy')
        // let locaTikuData = false;
        const testInfo = await wx.getStorageSync('testInfo')
        const sexfx = testInfo.sex == '女' ? '男' : (testInfo.sex == '男' ? '女' : '*')
        console.log('获取题库数据sexfx:', sexfx)
        if (locaTikuData) {
            // 过滤
            const locaTikuData2 = locaTikuData.filter((a) => a.gender !== sexfx)
            console.log('读取到题库：', locaTikuData2);
            const index = wx.getStorageSync('nowmydatiitemindex') || 0
            // 本地已经有数据
            this.setData({
                tikuList: locaTikuData2,
                itemtikuList: locaTikuData2[index],
                selectIndex: locaTikuData2[index].selectIndex,
                percentage: (locaTikuData2[index].index * 2.63).toFixed(0),
            })
        } else if (mytikuData_copy) {
            wx.setStorageSync('mytikuData', mytikuData_copy)
            // 点击重测测试后会到这里
            // 过滤
            const locaTikuData2 = mytikuData_copy.filter((a) => a.gender !== sexfx)
            console.log('读取到题库备份：', locaTikuData2);
            const index = wx.getStorageSync('nowmydatiitemindex') || 0
            // 本地已经有数据
            this.setData({
                tikuList: locaTikuData2,
                itemtikuList: locaTikuData2[index],
                selectIndex: locaTikuData2[index].selectIndex,
                percentage: (locaTikuData2[index].index * 2.63).toFixed(0),
            })
        } else {
            wx.showToast({
              title: '出错了！刷新后重试~',
              icon: 'none'
            })
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getdaanList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})